home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / kwmv.arc / LS.C < prev    next >
C/C++ Source or Header  |  1985-08-09  |  5KB  |  233 lines

  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5.     char attribute;
  6.     unsigned file_time;
  7.     unsigned file_date;
  8.     long file_size;
  9.     char file_name[13];
  10. } file_desc;
  11.  
  12. typedef struct
  13. {
  14.     char dos_reserved[21];
  15.     file_desc file;
  16. } fcb;
  17.  
  18. #define maxfiles 64 
  19.  
  20. char printbuf[256];
  21. int mode = 0x10;
  22. file_desc *get_first(),*get_next();
  23. char *index(), *rindex();
  24. int verbose=0,column=4,recurse=0;
  25.  
  26. main(argc,argv)
  27. char *argv[];
  28. {
  29.     int noargs;
  30.     char *current;
  31.     char namebuf[128];
  32.     /*
  33.      * set up a default search name
  34.      */
  35.     if (noargs = (argc == 1))
  36.         argc++;
  37.     while(--argc)
  38.     {
  39.         if (noargs)
  40.             current = "*.*";
  41.         else
  42.             current = *(++argv);    /* get current file name */
  43.         if (*current == '-')
  44.         {
  45.             ++current;    /* point past - */
  46.             while (*current)
  47.             {
  48.                 switch (*current++)
  49.                 {
  50.                 case 'l':
  51.                 case 'L':
  52.                     verbose = 1;
  53.                     if (column != 1)
  54.                         column = 2;
  55.                     break;
  56.                 case 'c':
  57.                 case 'C':
  58.                     column = 1;
  59.                     break;
  60.                 case 'a':
  61.                 case 'A':
  62.                     mode = 0x2 + 0x4 + 0x10;
  63.                     break;
  64.                 case 'r':
  65.                 case 'R':
  66.                     recurse = 1;
  67.                     mode = 0x2 + 0x4 + 0x10;
  68.                     break;
  69.                 default:
  70.                     break;
  71.                 }
  72.             }
  73.             /* if we're down to one argument after looking at all the
  74.                switches, we need to set noargs to true */
  75.             if (noargs = (argc == 1))
  76.                 argc++;
  77.             continue;
  78.         }
  79.         if (get_first(current)->attribute & 0x10 &&
  80.             NULL == index(current,'?') && NULL == index(current,'*'))
  81.         {
  82.             strcpy(namebuf,current);
  83.             strcat(namebuf,"\\*.*");
  84.             current = namebuf;
  85.         }
  86.         do_dir(current);
  87.     }
  88. }
  89.  
  90. do_dir(current)
  91.     char *current;
  92. {
  93.     file_desc files[maxfiles];    /* as many as we'll likely need */
  94.     file_desc *curr_file,*get_next();
  95.     unsigned int time,date;
  96.     int i,j,col;
  97.     int files_cmp();
  98.     char atts[4]; /* drw */
  99.     /* look for match */
  100.     i = 0;
  101.     if (!(curr_file = get_first(current)))
  102.     {
  103.         printf(stderr,"ls : no files matching %s\n",current);
  104.         return;    
  105.     }
  106.     files[i++] = *curr_file;
  107.     /* get all matching */
  108.     while ((curr_file = get_next()) && i < maxfiles)
  109.         files[i++] = *curr_file;    
  110.     if (i > 1)
  111.         qsort(files,i,sizeof(file_desc),files_cmp);
  112.     printf("%s\n",current);
  113.     col = 1;
  114.     for (j = 0; j < i; j++)
  115.     {
  116.         register char *c = files[j].file_name;
  117.         if (*c == '.')
  118.             continue;    /* filter out . and .. */
  119.         while (*c)
  120.         {
  121.             *c = tolower(*c);
  122.             c++;
  123.         }
  124.         if (verbose)
  125.         {
  126.             register char att = files[j].attribute;
  127.             atts[3] = 0;    /* terminate string */
  128.             atts[0] = att & 0x10 ? 'd' : '-';
  129.             atts[1] = att & 2 ? '-' : 'r';
  130.             atts[2] = att & 1 ? '-' : 'w';
  131.             if (atts[0] == 'd')
  132.             {
  133.                 register int k;
  134.                 sprintf(printbuf,"%s %s\\",atts,files[j].file_name);
  135.                 write(1,printbuf,strlen(printbuf));
  136.                 k = 12 - strlen(files[j].file_name)+7;
  137.                 while(k--)
  138.                     write(1," ",1);
  139.  
  140.             }
  141.             else
  142.             {
  143.                 sprintf(printbuf,"%s %-12s %-6ld ",atts,files[j].file_name,
  144.                         files[j].file_size);
  145.                 write(1,printbuf,strlen(printbuf));
  146.             }
  147.             time = files[j].file_time;
  148.             date = files[j].file_date;
  149.             sprintf(printbuf,"%02d/%02d %02d:%02d ",
  150.                 ((date >> 5) & 0x0F),    /* month */
  151.                 date & 0x1F,        /* day    */
  152.                 (time >> 11) & 0x1F,        /* hours */
  153.                 (time >> 5) & 0x3F);        /* minutes */
  154.             write(1,printbuf,strlen(printbuf));
  155.         }
  156.         else
  157.             if (files[j].attribute & 0x10)
  158.             {
  159.                 register int k;
  160.                 sprintf(printbuf,"%s\\",files[j].file_name);
  161.                 write(1,printbuf,strlen(printbuf));
  162.                 k = 16 - strlen(files[j].file_name);
  163.                 while(--k)
  164.                     write(1," ",1);
  165.  
  166.             }
  167.             else
  168.             {
  169.                 sprintf(printbuf,"%-13s   ",files[j].file_name);
  170.                 write(1,printbuf,strlen(printbuf));
  171.             }
  172.         if (col == column)
  173.         {
  174.             col = 1;
  175.             write(1,"\r\n",2);
  176.         }
  177.         else
  178.             col++;
  179.     }
  180.     write(1,"\r\n",2);
  181.     if (recurse)
  182.         for (j = 0; j < i; j++)
  183.         {
  184.             /* we've got a subdirectory */
  185.             if (files[j].attribute & 0x10 && files[j].file_name[0] != '.')
  186.             {
  187.                 char *path;
  188.                 char dirname[48];
  189.                 if (!strcmp(current,"*.*"))
  190.                     dirname[0] = '\0';
  191.                 else
  192.                     strcpy(dirname,current);
  193.                 if (path = rindex(dirname,'\\'))
  194.                     *(++path) = '\0';
  195.                 strcat(dirname,files[j].file_name);    /* get name */
  196.                 strcat(dirname,"\\*.*");
  197.                 do_dir(dirname);
  198.             }
  199.         }
  200. }
  201. files_cmp(a,b)
  202.     file_desc *a,*b;
  203. {
  204.     return strcmp(a->file_name,b->file_name);
  205. }
  206.  
  207. fcb tmp;
  208.  
  209. file_desc *get_first(fname)
  210.     char *fname;
  211. {
  212.     register int result;
  213.     /* set the disk transfer address */
  214.     bdos(0x1A,&tmp);
  215.     result = bdos(0x4E,fname,mode);
  216.     /* make the find first call */
  217.     if(2 == result || 18 == result)
  218.         return NULL;
  219.     return &(tmp.file);
  220. }
  221.  
  222. file_desc *get_next()
  223. {
  224.     register int result;
  225.     /* set the disk transfer address */
  226.     bdos(0x1A,&tmp);
  227.     result = bdos(0x4f,0,0);
  228.     /* make the find next call */
  229.     if (18 == result)
  230.         return NULL;
  231.     return &(tmp.file);
  232. }
  233.